package com.meshnetics.gb.stack.mac.type;

import com.meshnetics.gb.type.impl.zcl.Bitmap;

/**
 * Superframe specification bitmap, used in PANDescriptor.
 *
 * @author AKovylin
 */
public class SuperframeSpec extends Bitmap {
    public SuperframeSpec(int beaconOrder, int superframeOrder, int finalCAPSlot,
                          boolean batteryLifeExt, boolean panCoord, boolean associationPermit) {
        this(TypeSuperframeSpec.TYPE, new byte[]{0, 0});
        if (beaconOrder < 0 || beaconOrder > 15) {
            throw new IllegalArgumentException("beaconOrder is invalid");
        }
        if (superframeOrder < 0 || superframeOrder > 15) {
            throw new IllegalArgumentException("superframeOrder is invalid");
        }
        if (finalCAPSlot < 0 || finalCAPSlot > 15) {
            throw new IllegalArgumentException("finalCAPSlot is invalid");
        }
        super.setBit(12, batteryLifeExt);
        super.setBit(14, panCoord);
        super.setBit(15, associationPermit);
        super.setBits(0, 4, beaconOrder);
        super.setBits(4, 8, superframeOrder);
        super.setBits(8, 12, finalCAPSlot);
    }

    SuperframeSpec(TypeSuperframeSpec type, byte[] value) {
        super(type, value);
        if (value == null) {
            throw new NullPointerException("value is null");
        }
        if (value.length != 2) {
            throw new IllegalArgumentException("invalid value");
        }
        if (getBit(13)) {
            throw new IllegalArgumentException("reserved bit is set");
        }
    }

    public int getBeaconOrder() {
        return getBits(0, 4);
    }

    public int getSuperframeOrder() {
        return getBits(4, 8);
    }

    public int getFinalCAPSlot() {
        return getBits(8, 12);
    }

    public boolean isBatteryLifeExt() {
        return getBit(12);
    }

    public boolean isPANCoord() {
        return getBit(14);
    }

    public boolean isAssociationPermit() {
        return getBit(15);
    }
}
